<template>
    <el-row class="user-info-form" type="flex" justify="center">
        <el-col>
            <el-form label-width="100px" :model="setMaintainStandardForm" :rules="setMaintainStandardFormRules" ref="setMaintainStandardForm" @submit.native.prevent>
                <el-form-item label="名称：" prop="name">
                    <el-input v-model="setMaintainStandardForm.name" auto-complete="off" placeholder="请输入名称"></el-input>
                </el-form-item>
                <el-form-item label="备注：">
                    <el-input type="textarea" v-model="setMaintainStandardForm.remark" auto-complete="off" placeholder="请输入备注"></el-input>
                </el-form-item>
                <el-form-item label-width="0" class="verifyidentity-button">
                    <el-button @click.stop="closeDialogFun">取消</el-button>
                    <el-button type="primary" @click="saveSetMaintainStandard">新建</el-button>
                </el-form-item>
            </el-form>
        </el-col>
    </el-row>
</template>
<script>
import { updateMaintainStandard } from '@/api/maintence'

export default {
    name: 'NewSetMaintainStandard',
    props: {
        maintainStandardOptions: {
            type: Array,
            required: true
        },
        closeDialogFun: {
            type: Function,
            required: true
        }
    },
    data() {
        var checkMaintainStandardName = (rule, value, callback) => {
            if(value) {
                if (!this.checkNameRepeat(value)) {
                    callback(new Error('该名称已存在'))
                } else {
                    callback()
                }
            } else {
                callback(new Error('请输入维保标准名称'))
            }
        }
        return {
            setMaintainStandardForm: {
                name: '',
                remark: ''
            },
            setMaintainStandardFormRules: {
                name: [
                    { required: true, validator: checkMaintainStandardName, trigger: 'blur' }
                ]
            },
        }
    },
    methods: {
        checkNameRepeat(name) {
            let maintainStandardOptions = this.maintainStandardOptions
            if (name) {
                for(var i=0, len=maintainStandardOptions.length; i<len; i++) {
                    if(name === maintainStandardOptions[i].name) {
                        return false
                    }
                }
                return true
            }
        },
        // 新建
        saveSetMaintainStandard() {
            let that = this
            that.$refs['setMaintainStandardForm'].validate((valid) => {
                if (valid) {
                    let parames = {
                        name: that.setMaintainStandardForm.name,
                        remark: that.setMaintainStandardForm.remark
                    }
                    // 提交
                    updateMaintainStandard(parames).then(res => {
                        if(res.status === 200) {
                            that.$message({
                                message: '保存成功！',
                                type: 'success'
                            })
                            that.closeDialogFun(true)
                        } else {
                            that.$message({
                                message: res.info,
                                type: 'warning'
                            })
                        }
                    })
                } else {
                    return false
                }
            })
        }
    }
}
</script>
